﻿using DesignPattern.ViewModels.Pages;
using DesignPattern.ViewModels.Windows;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using System.Windows.Controls;
using Wpf.Ui;
using Wpf.Ui.Abstractions;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;

namespace DesignPattern.Views.Windows
{
    public partial class MainWindow : INavigationWindow
    {
        public MainWindowViewModel ViewModel { get; }
        public MainWindow(MainWindowViewModel viewModel, INavigationViewPageProvider navigationViewPageProvider,
            INavigationService navigationService, ISnackbarService snackbarService,
            IContentDialogService contentDialogService
        )
        {
            ViewModel = viewModel;
            DataContext = this;
            SystemThemeWatcher.Watch(this);
            InitializeComponent();
            SetPageService(navigationViewPageProvider);
            navigationService.SetNavigationControl(RootNavigation);
            snackbarService.SetSnackbarPresenter(SnackbarPresenter);
            contentDialogService.SetDialogHost(RootContentDialog);
            //由于Navigated事件在目标页面选中前触发，无法获取到目标页面类型，
            //不得不使用SelectionChanged事件
            RootNavigation.SelectionChanged += OnSelectionChanged;
            RootNavigation.Navigated += OnNavigated;
            //可以禁用默认的导航
            RootNavigation.Navigating += OnNavigating;
        }
        private void OnNavigating(NavigationView sender, NavigatingCancelEventArgs e)
        {   
            //禁用掉NavigationView 的默认导航机制
            // e.Cancel = true;
            // 使用 NavigationService进行带DataContext的导航，会自动将DataContext设置到目标页面上
            // var navigationService = App.Services.GetService<INavigationService>();
            // navigationService.Navigate(targetPageType, viewModel);
        }

        #region INavigationWindow methods

        public INavigationView GetNavigation() => RootNavigation;

        public bool Navigate(Type pageType) => RootNavigation.Navigate(pageType);

        public void SetPageService(INavigationViewPageProvider navigationViewPageProvider) =>
            RootNavigation.SetPageProviderService(navigationViewPageProvider);

        public void ShowWindow() => Show();

        public void CloseWindow() => Close();

        #endregion INavigationWindow methods

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Application.Current.Shutdown();
        }
        public void SetServiceProvider(IServiceProvider serviceProvider)
        {
            // 在WPF-UI 4.0中，这个方法可能是必需的，但我们不需要实现它
            // 因为我们已经在构造函数中设置了服务
        }
        //处理选择变更事件，进而获取到导航之后的页面
        private void OnSelectionChanged(object sender, RoutedEventArgs e)
        {
            if (sender is NavigationView navView && navView.SelectedItem is NavigationViewItem selectedItem)
            {
                // 检查目标页面类型是否是设计模式页面
                if (selectedItem.TargetPageType == typeof(Views.Pages.DesignPatternPage))
                {
                    // 从Tag获取设计模式名称
                    string patternName = null;
                    if (selectedItem.Tag is string tag && !string.IsNullOrEmpty(tag))
                    {
                        patternName = tag;
                    }

                    // 如果获取到了有效的模式名称
                    if (!string.IsNullOrEmpty(patternName))
                    {
                        // 从容器中获取ViewModel单例实例
                        var viewModel = App.Services.GetService<DesignPatternViewModel>();
                        // 调用加载模式方法
                        viewModel?.LoadPattern(patternName);
                    }
                }
            }
        }
        private void OnNavigated(NavigationView sender, NavigatedEventArgs e)
        {
            // var navigationView = (NavigationView)sender;
            // //  处理导航之前的页面
            // var selectedItem = sender.SelectedItem as NavigationViewItem;
            // if (selectedItem != null)
            // {
            //     // 使用 selectedItem 的属性，如 Content, Tag 等
            //     var itemContent = selectedItem.Content;
            // }
            // // 处理导航到的目标页面e.Page
            // // Page对象，而不是触发导航的NavigationViewItem
            // Console.WriteLine($"{e.Page.GetType().Name}");
        }
    }
}